<?php
namespace admin;

use models\BaseDoo;

class Index extends Admin{
    //使用构造函数的时候，需要调用父类的构造方法
    public function __construct()
{
        $this->assign('lh_php','LHPHP');
        parent::__construct();
    }
    //后台首页
    public function index(){
        $this->assign('title','后台首页');
        $dataBase = new BaseDoo();
        //查找订单总量
        $order_count = $dataBase->count('order',['supplier_id'=>$_SESSION['id']]);

        //订单总价
        $order_total = $dataBase->sum('order','money',['supplier_id'=>$_SESSION['id']]);
        //格式化数字
        $order_total = number_format($order_total,2,'.',',');

        //客户总数
        $custom_total = $dataBase->count('cooperation_supplier',['supplier_id'=>$_SESSION['id']]);

        //今日总收入
        #获取今天的时间
        $today = strtotime(date('Y-m-d'));
        #获取昨天的时间
        $today_last = strtotime(date('Y-m-d',strtotime('-1 day')));
        $today_money = $dataBase->sum('order','money',['supplier_id'=>$_SESSION['id'],'create_time[>=]'=>$today_last,'create_time[<=]'=>$today]);
        if(!$today_money){
            $today_money = 0;
        }
        $today_money = number_format($today_money,2,'.',',');

        //获取合作的客户列表
        $coop_list = $dataBase->select('cooperation_supplier',
        ['id','client_id','supplier_id','create_time','pur_abbre','status'],
        ['supplier_id' => $_SESSION['id']]);
        $sort_id = 1;
      
        foreach($coop_list as &$val){
            //获取客户信息
            $clientFind = $dataBase->get('client',['client_abbre','client_name'],['id'=>$val['client_id']]);
            $name_show = !!$clientFind->client_abbre ? $clientFind->client_abbre : '暂无名称';
            $val['sort_id'] = $sort_id;
            $val['client_abbre'] = $name_show;
            $val['create_time'] = date('Y-m-d',$val['create_time']);
            $val['status_name'] = $this->getStatusName($val['status']);
            $sort_id++;
        }
        $info['order_count'] = $order_count;
        $info['order_total'] = $order_total;
        $info['custom_total'] = $custom_total;
        $info['today_money'] = $today_money;
        $info['coop_list'] = $coop_list;
        $this->assign($info);
        $this->display('index/index');
    }

    function getStatusName($status=''){
        if($status == ''){
            return '无状态';
        }
        $status_arr = [-1=>'待审核',1=>'合作',0=>'暂停'];
        return $status_arr[$status];
    }
    
}